package net.zretc.controller;

import net.zretc.entity.Product;
import net.zretc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping
    public List<Product> findAll() {
        try {
            return productService.findAll();
        } catch (Exception e) {
            System.err.println("获取商品列表出错: " + e.getMessage());
            return null;
        }
    }

    @PostMapping
    public int insert(@RequestBody Product product) {
        try {
            return productService.insert(product);
        } catch (Exception e) {
            System.err.println("插入商品出错: " + e.getMessage());
            return 0;
        }
    }

    @PutMapping
    public int update(@RequestBody Product product) {
        try {
            return productService.update(product);
        } catch (Exception e) {
            System.err.println("更新商品出错: " + e.getMessage());
            return 0;
        }
    }

    @DeleteMapping("/{productId}")
    public int delete(@PathVariable Integer productId) {
        try {
            return productService.delete(productId);
        } catch (Exception e) {
            System.err.println("删除商品出错: " + e.getMessage());
            return 0;
        }
    }
}